﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 用于控制逃跑球和障碍球的运动

public class TowardsMovement : MonoBehaviour
{
    public float speed;
    public float thresh;


    private Transform target;
    private Rigidbody ballRigidbody;
    private const float refreshGap = 3.0f;
    private float timer;
    private Vector3 currentForce;

    // Start is called before the first frame update
    void Start()
    {
        target = GameObject.Find("HeroBall").transform;
        ballRigidbody = GetComponent<Rigidbody>();
        timer = 0.0f;
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 offset = target.position - transform.position;
        if (offset.magnitude <= thresh)
        {
            ballRigidbody.AddForce(offset.normalized * speed);
        }
        else
        {
            if (timer <= 0.0f)
            {
                currentForce = new Vector3(Random.Range(-1.0f, 1.0f), 0, Random.Range(-1.0f, 1.0f)).normalized * speed;
                timer = refreshGap;
            }
            ballRigidbody.AddForce(currentForce);
            timer -= Time.deltaTime;
        }
    }
}
